package com.sntk.organauthomanager.domain.model;

import com.sntk.organauthomanager.infrastructure.exception.DataNoLegalException;
import lombok.Getter;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @ClassName: RoleResource
 * @author: wgchao
 * @createTime: 2020/7/20 4:12 PM
 * @Description: 角色资源
 */
@Getter
public class RoleResource extends AggregateRoot {
    //唯一标识
    private RoleIdentify roleIdentify;

    private Set<String> resources;

    public RoleResource(RoleIdentify roleIdentify, Set<String> resources) {
        if(roleIdentify == null){
            throw new DataNoLegalException("角色缺失");
        }
        this.roleIdentify = roleIdentify;
        this.resources = resources;
    }

    public RoleResource(Organization organization, Role role, List<Resource> resources){
        organization.normalException();
        role.normalException();
        this.roleIdentify = new RoleIdentify(organization.getInstitutionNumber(),role.getRoleIdentify().getRoleCode());
        if(resources==null || resources.isEmpty()){
            this.resources = null;
        }
        this.resources = new HashSet<>();
        for(Resource resource : resources){
            resource.normalException();
            this.resources.add(resource.getMark());
        }
    }

}
